package com.jy.rhin.support.interceptor.convertor;

import cn.org.chiss.rhin._2015.document.management.DocumentStoredQuery;
import cn.org.chiss.rhin._2015.document.management.DocumentStoredQueryResponse;
import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 健康档案存储服务-获取健康档案
 * @Author: lww
 * @Date: 2022/5/31 15:19
 */
@Service("AuditInfoConverter4TerminologyDocumentStoredQuery")
public class AuditInfoConverter4TerminologyDocumentStoredQuery extends BaseAuditInfoConverter<DocumentStoredQuery, DocumentStoredQueryResponse> {

    public AuditInfoConverter4TerminologyDocumentStoredQuery() {
        super(EventActionCodeEnum.READ,
                ServiceCodeEnum.DOCUMENTS_MANAGEMENT,
                EventTypeCodeEnum.DOCUMENT_STORED_QUERY,
                true,
                ParticipantObjectIDTypeCodeEnum.SEARCH_CRITERIA,
                ParticipantObjectTypeCodeRoleEnum.MASTER_FILE,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     *
     * @return
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO 从安全节点验证服务，获取节点OID
        auditSource.setBiaoShi("documents-management-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.DOCUMENTS_STORAGE.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(DocumentStoredQuery documentStoredQuery, DocumentStoredQueryResponse documentStoredQueryResponse) {
        return new AuditParticipantUserDesc("client-2", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(DocumentStoredQuery documentStoredQuery, DocumentStoredQueryResponse documentStoredQueryResponse) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(DocumentStoredQuery documentStoredQuery, DocumentStoredQueryResponse documentStoredQueryResponse) {
        return new AuditParticipantUserDesc("documents-management-service", ServiceCodeEnum.DOCUMENTS_MANAGEMENT.getName());
    }

    @Override
    String getObjectId(DocumentStoredQuery documentStoredQuery, DocumentStoredQueryResponse documentStoredQueryResponse) {
        if (Objects.nonNull(documentStoredQuery)
                && Objects.nonNull(documentStoredQuery.getAdhocQuery())
                && Objects.nonNull(documentStoredQuery.getAdhocQuery().getId())) {
            return documentStoredQuery.getAdhocQuery().getId().getValue();
        }
        if (Objects.nonNull(documentStoredQueryResponse)
                && !CollectionUtils.isEmpty(documentStoredQueryResponse.getSubmissionSet())) {
            return documentStoredQueryResponse.getSubmissionSet().stream().map(valueSet -> valueSet.getId().getValue()).collect(Collectors.joining(","));
        }
        return "";
    }
    
    @Override
	public boolean isMatch(String method, Object data) {
		return "DocumentRetrievePortImpl.documentStoredQuery".equals(method);
	}
}
